package cn.yj.core.business.controller;

import cn.yj.cgs.dba.entity.Customer;
import cn.yj.cgs.dba.entity.FeedBack;
import cn.yj.cgs.dba.entity.Message;
import cn.yj.common.AppExecutor;
import cn.yj.common.Enums;
import cn.yj.syslog.common.LogType;
import cn.yj.syslog.common.OperateLog;
import cn.yj.config.web.AppBaseController;
import cn.yj.core.business.service.CustomerService;
import cn.yj.core.business.service.iservice.IMessageService;
import cn.yj.entity.R;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * @author 永健
 * @since 2021-06-05 11:32
 */
@RestController
@RequestMapping("/feedback")
public class FeedBackController extends AppBaseController<FeedBack> {


    @Resource
    private IMessageService iMessageService;

    @Resource
    private CustomerService customerService;

    @OperateLog(describe = "小程序反馈",type = LogType.UPDATE)
    @PostMapping("/save")
    public R save(@RequestBody @Valid FeedBack feedBack) {
        feedBack.setUserCode(getUserCode());
        feedBack.setRealName(((Customer) getCurrentUser()).getRealName());
        AppExecutor.exec(() -> {
            List<Customer> list = customerService.list(new QueryWrapper<Customer>().lambda().eq(Customer::getIsAdmin,
                    Enums.Common.TRUE));
            Message message = new Message();
            message.setContent("反馈用户："+feedBack.getRealName()+"\n反馈内容："+feedBack.getContent());
            message.setTitle("系统反馈");
            list.forEach(c -> {
                message.setUserCode(c.getUserCode());
                iMessageService.notice(message);
            });
        });
        return success(feedBack.insert());
    }
}
